// packageH/chitchat/chatRecordDetail/chatRecordDetail.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    history_queue_id:null,
    date_str:'',
    chats_list:[],
    emojiObjs:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     this.setData({
      emojiObjs:JSON.parse(decodeURIComponent(options.emojiObjs))
     })
     this.getChatInfo(options.history_queue_id)
     wx.setNavigationBarTitle({
      title: '聊天记录',
    });
  },

  getChatInfo(history_queue_id){
    let url = app.getNetAddresss("plugin.yun-chat.frontend.chat.get-history-chat-list");
    let json = {
      history_queue_id:history_queue_id
    }
    app._postNetWork({
      url: url,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return app.tips(res.msg);
        let chat_list =  this.fmtChatList(res.data.chats_list)
        this.setData({
          chats_list : res.data.chats_list,
          date_str : res.data.date_str
        });
      },
    });
  },
  viewPicture(e) {
    let src = e.currentTarget.dataset.src;
    let arr = [];
    let chatList = this.data.chatList;
    chatList.forEach((item) => {
      if (item.content_type == 1) {
        arr.push(item.content);
      }
    });
    wx.previewImage({
      urls: arr,
      current: src,
    });
    console.log(src, this.data.chatList);
  },
  fmtChatList(chatList) {
    // 处理商品或表情消息
    if (chatList instanceof Array) {
      for (let i = 0; i < chatList.length; i++) {
        if (chatList[i]["content_type"] == 2 || chatList[i]["content_type"] == 3) {
          chatList[i]["content"] = JSON.parse(decodeURIComponent(chatList[i]["content"]));
        } else if (chatList[i]["content_type"] == 0) {
          chatList[i]["content"] = this.replaceEmoji(chatList[i]["content"]);
        }
      }
    } else if (chatList instanceof Object) {
      if (chatList["content_type"] == 2 || chatList["content_type"] == 3) {
        chatList["content"] = JSON.parse(decodeURIComponent(chatList["content"]));
      } else if (chatList["content_type"] == 0) {
        chatList["content"] = this.replaceEmoji(chatList["content"]);
      }
    }

    return chatList;
  },
  replaceEmoji(content) {
    let that = this
    return content.replace(/face\[([^\s\[\]]+?)\]/g, function (i) {
      return '<img src="' + that.data.emojiObjs[i]["url"] + '" style="width:30px;height:30px;"/>';
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})